package ac.zlf.maven.template.util;

import ac.zlf.maven.template.constant.MessageConstant;
import ac.zlf.maven.template.constant.SystemConstant;
import ac.zlf.maven.template.controller.ParentController;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Dialogs;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 * Created by Administrator on 2018/6/16.
 */
public class Util {
    /**
     *   打开一个新的窗口禁用之前的窗口
     *
     *
     * @param parentStage 父窗口的stage
     * @param viewFxmlPath  fxml文件路径 如：view/Main.fxml
     * @param title 窗口标题
     * @param data  传入窗口的数据
     * @param value 关闭窗口执行的方法
     */
    public static void openNewWindow(Stage parentStage, String viewFxmlPath, String title, Object data, EventHandler<WindowEvent> value){
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(Util.class.getClassLoader().getResource(viewFxmlPath));
        try {
            Parent root = fxmlLoader.load();
            Stage stage = new Stage();

            ParentController controller = fxmlLoader.getController();
            controller.setStage(stage);
            controller.setData(data);

            stage.setScene(new Scene(root, SystemConstant.DEFAULT_WINDOW_WIDTH, SystemConstant.DEFAULT_WINDOW_HEIGHT));
            stage.setTitle(title);
            stage.initModality(Modality.APPLICATION_MODAL);
            stage.setOnCloseRequest(value);
            stage.show();
        } catch (Exception e) {
            e.printStackTrace();
            Dialogs.showErrorDialog(parentStage, e.getMessage(), MessageConstant.ERROR_NEW_WINDOW, "Error", e);
        }
    }
}
